<?php
$ids = array(
    '西安新闻综合' => '1',
    '西安都市频道' => '2',
    '西安商务咨询' => '3',
    '西安戏剧影视' => '4',
    '西安丝路频道' => '5'
    '西安乐购购物' => '6',
    '西安移动电视' => '7'
);
if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $url = 'https://stream.xiancity.cn/live/' . $id . '/index.m3u8';
    $headers = array(
        'Referer: https://v.xiancity.cn/',
        'User-Agent: AppleCoreMedia/1.0.0.15F79 (iPhone; U; CPU OS 11_4 like Mac OS X; zh_cn)'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    curl_close($ch);
    if ($response === false) {
        exit;
    }
    $lines = explode("\n", $response);
    foreach ($lines as &$line) {
        if (strpos($line, '.ts') !== false) {
            $line = 'https://stream.xiancity.cn/live/' . $id . '/' . $line;
        }
    }
    header('Content-Type: application/x-mpegURL');
    echo implode("\n", $lines);
} else {
    header('Content-Type: text/html');
    foreach ($ids as $name => $id) {
        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?id=' . $id;
        echo $name . ', <a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($url) . '</a><br>';
    }
}
?>